﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //页面首次加载且商品ID不为空
        if (!IsPostBack & Request.QueryString["GoodsID"] != null)
        {

            MyDataDataContext db = new MyDataDataContext();

            //获取要修改的求购信息的记录
            var goods = (from r in db.LookingOfGoods
                         where r.GoodsID == Request.QueryString["GoodsID"]
                         select r).First();

            //显示求购信息
            //商品名称
            GoodsName.Text = goods.GoodsName;
            //商品描述
            GoodsDescription.Text = goods.GoodsDescription;
            //商品价格下限
            PriceFloor.Text = Convert.ToString(goods.PriceFloor);
            //商品价格上限
            PriceCap.Text = Convert.ToString(goods.PriceCap);
            //商品分类
            Type.SelectedValue = goods.Type;

            //获取商品分类信息
            var results = from r in db.GoodsType
                          select new
                          {
                              r.GoodsTypeName
                          };//映射需要显示的字段

            //设置DropDownList控件的数据源
            Type.DataSource = results;
            //调用DataBind()方法显示数据
            Type.DataBind();

        }
    }

    //删除商品
    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        //不需要检查控件验证是否通过
        //获取要删除的对象
        MyDataDataContext db = new MyDataDataContext();
        LookingOfGoods DeleteObj = (from r in db.LookingOfGoods
                                    where r.GoodsID == Request.QueryString["GoodsID"]
                                    select r).FirstOrDefault();

        //删除
        db.LookingOfGoods.DeleteOnSubmit(DeleteObj);
        db.SubmitChanges();

        //返回信息列表页面
        Response.Redirect("ChangeInformation2.aspx");
    }

    //修改商品信息
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        //控件全部通过验证
        if (Page.IsValid)
        {
            MyDataDataContext db = new MyDataDataContext();
            //获取要修改的商品记录
            LookingOfGoods goods1 = (from r in db.LookingOfGoods
                                     where r.GoodsID == Request.QueryString["GoodsID"]
                                     select r).FirstOrDefault();


            //获取用户修改的内容并提交到数据库
            //商品名称
            goods1.GoodsName = GoodsName.Text.Trim();
            //商品描述
            goods1.GoodsDescription = GoodsDescription.Text.Trim();
            //商品价格下限
            goods1.PriceFloor = double.Parse(PriceFloor.Text.Trim());
            //商品价格上限
            goods1.PriceCap = double.Parse(PriceCap.Text.Trim());
            //商品类型
            goods1.Type = Type.SelectedValue;
            //根据商品类型名找出对应的商品类型ID
            GoodsType typeID = (from r in db.GoodsType
                                where r.GoodsTypeName == Type.SelectedValue
                                select r).First();
            //商品类型ID
            goods1.GoodsTypeID = typeID.GoodsTypeID;
            //修改时间,格式为 年/月/日 时:分:秒
            goods1.ReleaseTime = DateTime.Now.ToString();

            //提交
            db.SubmitChanges();

            //返回信息列表页面
            Response.Redirect("ChangeInformation2.aspx");
        }
    }
}